//
//  BDFaceForUploadImageViewController.m
//  MingJingSalesAssistant
//
//  Created by v_bihongvwei on 2022/1/20.
//

#import "BDFaceForUploadImageViewController.h"
#import "BDFaceVerificationController.h"
@interface BDFaceForUploadImageViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate>
// 下一步button
@property (weak, nonatomic) IBOutlet UIButton *nextButton;
// 本地选择图片
@property (weak, nonatomic) IBOutlet UIImageView *localImageVIew;
// 图片整个大的点击区域button
@property (weak, nonatomic) IBOutlet UIButton *imgBigButton;
// 小加号button
@property (weak, nonatomic) IBOutlet UIButton *smallBtn;
// 旋转button
@property (weak, nonatomic) IBOutlet UIButton *rotatingButton;
// 旋转角速
@property (assign, nonatomic) CGFloat degreeData;
// 图片base64
@property(copy, nonatomic) NSString *encodedImageString;

@end

@implementation BDFaceForUploadImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.nextButton.backgroundColor = [UIColor colorWithRed:0 green: 128 / 255.0 blue:255 / 255.0 alpha: 0.29];
    self.nextButton.enabled = false;
    self.imgBigButton.hidden = YES;
    _degreeData = 0.0;
    
    // 添加手势返回
    UISwipeGestureRecognizer *recognizer;
    //right--
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [[self view] addGestureRecognizer:recognizer];
}

// 手势返回方法
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
        //执行程序
        [self.navigationController popViewControllerAnimated: YES];
    }
}

- (IBAction)goBackAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)loadImageAction:(UIButton *)sender {
    [self selectPhoto];
}

/**
 下一步按钮事件
 */
- (IBAction)nextStepAction:(UIButton *)sender {
    // 开始人脸对比图片流程
    self.action(@{@"register_image":self.encodedImageString});
}


// 从相册选择照片
- (void)selectPhoto {
    // 创建UIImagePickerController
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    // 设置代理
    imagePickerController.delegate = self;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    imagePickerController.allowsEditing = NO;
    [self presentViewController: imagePickerController animated: YES completion: nil];
}

// 相册回调方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info {
    // 选择完成后返回原来控制器
    [picker dismissViewControllerAnimated: YES completion: nil];
    // 从info中取出图片复制给对应的imageview
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.localImageVIew.image = image;
    NSData *imageData = UIImageJPEGRepresentation(image, 1);
    self.encodedImageString = [imageData base64EncodedStringWithOptions:0];
    if (image == nil) {
        self.smallBtn.hidden = NO;
        self.imgBigButton.hidden = YES;
        self.localImageVIew.backgroundColor = [UIColor clearColor];
        self.nextButton.enabled = NO;
        self.localImageVIew.backgroundColor = [UIColor colorWithRed:229 / 255.0 green:242 /255.0 blue:255 /255.0 alpha:0.29];
    } else {
        self.rotatingButton.hidden = NO;
        self.smallBtn.hidden = YES;
        self.imgBigButton.hidden = NO;
        self.localImageVIew.backgroundColor = [UIColor colorWithRed:229 / 255.0 green:242 /255.0 blue:255 /255.0 alpha:1];
        self.localImageVIew.layer.cornerRadius = 10;
        self.localImageVIew.clipsToBounds = YES;
        self.localImageVIew.layer.borderWidth = 1.5;
        self.localImageVIew.layer.borderColor = [UIColor colorWithRed: 159 / 255.0 green: 200 / 255.0 blue: 245 / 255.0 alpha:1].CGColor;
        self.nextButton.backgroundColor = [UIColor colorWithRed:0 green: 128 / 255.0 blue:255 / 255.0 alpha:1];
        self.nextButton.enabled = YES;
    }
}

/**
 点击图片按钮
 */
- (IBAction)imgAction:(UIButton *)sender {
    [self selectPhoto];
}

/**
 点击旋转按钮
 */
- (IBAction)rotatingAction:(UIButton *)sender {
    self.degreeData += 0.5;
    if (_degreeData == 2.0) {
        _degreeData = 0.0;
    }
    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI * _degreeData);
    self.localImageVIew.transform = transform;
}

@end
